//
//  HomeCategoryView.m
//  DoulinClient
//
//  Created by ypj on 2021/5/28.
//

#import "HomeCategoryView.h"
#import "HomeCategoryItemCell.h"
@interface HomeCategoryView ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, assign) CGFloat itemW;
@property (nonatomic, assign) CGFloat itemH;
@property (nonatomic, assign) BOOL jumped;
@end
@implementation HomeCategoryView

- (void)awakeFromNib{
    [super awakeFromNib];
    self.itemW = floor((ScreenWidth - 30.0)/4.0);
    self.itemH = 70;
    [_collectionView createBordersWithColor:nil withCornerRadius:6.0 andWidth:0];
    [self.bgVIew createBordersWithColor:nil withCornerRadius:6.0 andWidth:0];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 1;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(1, 0, 1, 0);
    layout.itemSize = CGSizeMake(self.itemW, self.itemH);
    _collectionView.collectionViewLayout = layout;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [_collectionView registerNib:kNib(HomeCategoryItemCell) forCellWithReuseIdentifier:kClsID(HomeCategoryItemCell)];
    self.collectionView.showsVerticalScrollIndicator=NO;
    self.collectionView.showsHorizontalScrollIndicator=NO;
}
+ (instancetype)shareInstance{
    return [[NSBundle mainBundle] loadNibNamed:@"HomeCategoryView" owner:nil options:nil][0];
}
- (void)setDataAry:(QuanXianModel*)model{
    self.model = model;
    [self.collectionView reloadData];
    [self getCollectionViewHeight];
    [self.collectionView setContentOffset:CGPointZero animated:NO];
}
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    self.jumped = NO;
//}
//- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
//    self.jumped = NO;
//}
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
//    if (decelerate == NO) {//crollView停止滚动，完全静止
//        //NSLog(@"<-- %s, %d --> %d", __FUNCTION__, __LINE__, decelerate);
//        self.jumped = NO;
//    } else {//用户停止拖拽，但是scrollView由于惯性，会继续滚动，并且减速
//        //NSLog(@"<-- %s, %d --> %d", __FUNCTION__, __LINE__, decelerate);
//    }
//}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    if ([_model.name isEqualToString:@"车管权限"]) {
//        return 2;
//    }else if ([_model.name isEqualToString:@"销售权限"]){
//        return 4;
//    } else {
//        return 0;;
//    }
    return self.model.children.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeCategoryItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kClsID(HomeCategoryItemCell) forIndexPath:indexPath];
    QuanXianSmallModel *smallModel = self.model.children[indexPath.row];
    cell.titleLb.text = smallModel.name;
//  smallModel.icon = [smallModel.icon stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [cell.iconImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HTTP_URL,[NSString isChinese:smallModel.icon]]] placeholderImage:Image(@"default_icon")];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.itemBlk){
        self.itemBlk(indexPath);
    }
}
-(CGFloat)getCollectionViewHeight{
   CGFloat photoWidth = (ScreenWidth - 30.0) / 4.0;
    _collectionViewHeight = 0;
    NSInteger number = self.model.children.count % 4;
    NSInteger zomeNumber = self.model.children.count / 4;
    if (number == 0){
        number = zomeNumber;
    } else {
        number = zomeNumber + 1;
    }
    _collectionViewHeight =_collectionViewHeight+(number * photoWidth);
    _collectionView.height =_collectionViewHeight;
    _bgVIew.height = _collectionViewHeight;
     [_collectionView reloadData];
    return _collectionViewHeight;
}
@end
